package fr.vahren.processing.gui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fr.vahren.processing.Main;

public class MainFrame extends JFrame{
	private Main sketch;

	public MainFrame() {
		this.setSize(Main.SIZE, Main.SIZE); // The window Dimensions
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBounds(0, 0, Main.SIZE, Main.SIZE);
		sketch = new Main();
		JLabel label = new JLabel("LOADING...");
		label.setBounds(0, 0, Main.SIZE, 20);
		panel.add(label,BorderLayout.PAGE_START);
		panel.add(sketch,BorderLayout.CENTER);
		this.add(panel);
		this.setVisible(true);
		new LoadingThread(sketch, panel).start();
		sketch.init();
	}

	public static void main(String[] args) {
		new MainFrame().setVisible(true);

	}

	private class LoadingThread extends Thread {

		private Main proc;
		private JPanel panel;

		public LoadingThread(Main proc,JPanel panel) {
			this.proc = proc;
			this.panel = panel;
		}

		@Override
		public void run() {
			synchronized (proc) {
				while (!proc.ok) {
					System.out.println("waiting");
					try {
						proc.wait();
					} catch (InterruptedException e) {
						// interrupted!
						System.err.println("interrupted!");
					}
				}
				System.out.println("lets go");
				panel.remove(((BorderLayout)panel.getLayout()).getLayoutComponent(BorderLayout.PAGE_START));
				panel.validate();
				return;
			}

		}

	}
}
